package com.meisupic.copyright.controller;

import static com.meisupic.copyright.common.Constants.OSS_HOST;
import static com.meisupic.copyright.common.Constants.RIGHT_OWN_MODE_LEGAL_WORKS;

import com.meisupic.copyright.common.Constants;
import com.meisupic.copyright.domain.po.CopyRightWorks;
import com.meisupic.copyright.domain.vo.CopyRightWorksVo;
import com.meisupic.copyright.domain.vo.WorksAuditVo;
import com.meisupic.copyright.notify.NotifyQueue;
import com.meisupic.copyright.service.IContractRecordsService;
import com.meisupic.copyright.service.ICopyRightWorksService;
import com.meisupic.copyright.service.IDeclarationFilesService;
import com.meisupic.copyright.support.Message;
import com.meisupic.copyright.support.PdfToImage;
import com.meisupic.copyright.util.BeanMapper;
import com.meisupic.copyright.util.StringUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wujun
 * @version V1.0
 * @Description: TODO
 * @date 2018/3/2 7:37
 */
@RestController
public class UserController extends BaseController {

  @Autowired
  private IDeclarationFilesService declarationFilesService;

  @Autowired
  private ICopyRightWorksService copyRightWorksService;

  @Autowired
  private IContractRecordsService contractRecordsService;

  private String DECLARATION_FILE_PATH = OSS_HOST + Constants.DECLARATION_FILE_PATH;


  @Autowired
  NotifyQueue notifyQueue;

  @PostMapping("generatePdf")
  public Message generatePdf(@RequestBody CopyRightWorksVo copyRightWorksVo) {
    CopyRightWorks copyRightWorks = BeanMapper.map(copyRightWorksVo, CopyRightWorks.class);
    return Message.success(null);
  }

  @GetMapping("pdf2Img")
  public Message pdf2Img() throws IOException {
    File srcFile = new File(FileUtils.getUserDirectory(),
        "C20180000000000000056572800000008stp.pdf");
    FileUtils.copyURLToFile(new URL(
            "http://meisudci.oss-cn-beijing.aliyuncs.com/certificate/C20180000000000000056572800000008stp.pdf"),
        srcFile);
    PdfToImage pdfToImage = new PdfToImage();
    pdfToImage.pdfToImage(srcFile,
        new File(FileUtils.getUserDirectoryPath(), "C20180000000000000056572800000008stp.jpg"),
        null, 96f, false);
    return Message.success(null);
  }

  @GetMapping("execute")
  public String extask() {
    notifyQueue.addElementToList(new HashMap<>(), 1, "http://www.meisubq.com/o");
    return "{}";
  }

  @GetMapping("generatePdfById")
  public Message generatePdfById(Long id) {
    CopyRightWorks copyRightWorks = copyRightWorksService.findById(id);
    if (copyRightWorks == null) {
      return Message.error("未找到登记数据");
    }
    String statementSuffix = StringUtil.get32UUID();
    copyRightWorks.setOriginalStatement(statementSuffix + "_1.pdf");
    copyRightWorks.setCreativeStatement(statementSuffix + "_2.pdf");
    if (RIGHT_OWN_MODE_LEGAL_WORKS == copyRightWorks.getRightOwnMode()) {
      copyRightWorks.setLegalCreationStatement(statementSuffix + "_3.pdf");
    }
    declarationFilesService.generateFiles(copyRightWorks, null);

    copyRightWorks
        .setOriginalStatement(DECLARATION_FILE_PATH + copyRightWorks.getOriginalStatement());
    copyRightWorks
        .setCreativeStatement(DECLARATION_FILE_PATH + copyRightWorks.getCreativeStatement());

    if (copyRightWorks.getLegalCreationStatement() != null) {
      copyRightWorks.setLegalCreationStatement(
          DECLARATION_FILE_PATH + copyRightWorks.getLegalCreationStatement());
    }
    copyRightWorksService.updateWorks(copyRightWorks);
    return Message.success(id);
  }

  @GetMapping("pushWorks")
  public Message pushWorks(WorksAuditVo worksAuditVo) {
    copyRightWorksService.doPass(worksAuditVo);
    return Message.success(worksAuditVo.getId());
  }

  @GetMapping("applyRCode")
  public Message applyRcode(Long id) {

    copyRightWorksService.sendRcodeApply(copyRightWorksService.findById(id));
    return Message.success(null);
  }

  @GetMapping("contract/record")
  public Message RecordContract(Integer downloadId) {

    contractRecordsService.generateContract(downloadId);
    return Message.success(null);
  }

  @PostMapping("/generateCustomPdf")
  public Message generateCustomPdf(String creativePurpose, String creativeProcess, String creative,
      String publishStatus, Long copyrightWorksId) {

    CopyRightWorks po = copyRightWorksService.findById(copyrightWorksId);
    String creativeStatement = po.getCreativeStatement();
    String fileName = creativeStatement.substring(creativeStatement.lastIndexOf("/"), creativeStatement.length());
    declarationFilesService.generateFiles(creativePurpose, creativeProcess, creative, publishStatus, po.getAuthor(), fileName);
    return Message.success(null);
  }
}
